﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using Microsoft.IdentityModel.Tokens;

namespace Himp.Platform.Cores
{
    /// <summary>
    /// 部门服务
    /// </summary>
    public class DeptService : CrudAppService<Dept, DeptDto, Guid, DeptInput, DeptCreateDto, DeptUpdateDto>
    {
        private IRepository<Dept, Guid> _deptRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deptRepository"></param>
        public DeptService(IRepository<Dept, Guid> deptRepository) : base(deptRepository)
        {
            _deptRepository = deptRepository;
        }

        /// <summary>
        /// 获取门诊排班计划
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<DeptDto>> GetListAsync(DeptInput input)
        {
            var iQuery = await _deptRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.DeptCodg), i => i.DeptCodg == input.DeptCodg);

            if (input.Pi == 0)
            {
                var items = iQuery.ToList();
                var itemsDto = ObjectMapper.Map<List<Dept>, List<DeptDto>>(items.ToList());

                return new PagedResultDto<DeptDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<Dept>, List<DeptDto>>(items.ToList());

                return new PagedResultDto<DeptDto>(totalCount, itemsDto);
            }
        }
    }
}
